#!/usr/bin/env python 2.7
# -*- coding: utf-8 -*-
# @Time      :2019/1/22
# @Author    :Tian Maohong
# @Email     :tianmaohong@bbdservice.com
# @File      :IC25_OverdueSummaryIsNa
# @Software  :PyCharm

import json
import re
import sys
import datetime

reload(sys)
sys.setdefaultencoding('utf-8')

'''eof
name:逾期信息汇总表是否缺失
code:IC25_OverdueSummaryIsNa
tableName:
columnName:
groups:个人征信模块
dependencies:IC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


def inquireIsReportNo(data):
    """
    判断是否存在reportNo
    :param data:
    :return:True/False
    """
    try:

        is_reportNo = True if len(data.get("reportNo")) > 0 else False

    except:
        is_reportNo = False

    return is_reportNo


def formatData(table_Name):
    """
    获取数据
    :param table_Name:字典keys
    :return:[{}]
    """
    try:
        ic_data = IC_DSJ["data"].get(table_Name)
        return ic_data if isinstance(ic_data, list) and len(ic_data) > 0 else [{}]

    except:
        return [{}]


def convertDataType(data_value, data_type):
    """
    数据格式转换
    :param data_value:
    :param data_type: float/int/str/date_time
    :return:
    """
    data_value = str(data_value)
    return_data = None
    # float
    if data_type == 'float':
        try:
            return_data = float(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # int
    elif data_type == 'int':
        try:
            return_data = int(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # str
    elif data_type == 'str':
        try:
            return_data = str(data_value) if len(data_value) >= 0 else None
        except:
            return_data = None
    # date_time
    elif data_type == 'date_time':
        r = re.compile(r'\D+')
        try:
            return_data = datetime.datetime.strptime(data_value, '%Y.%m.%d').strftime('%Y-%m-%d')
        except:
            try:
                return_data = datetime.datetime.strptime(data_value, '%Y-%m-%d').strftime('%Y-%m-%d')
            except:
                try:
                    return_data = datetime.datetime.strptime(data_value, '%Y/%m/%d').strftime('%Y-%m-%d')
                except:
                    try:
                        data_value = r.sub('', data_value)
                        return_data = datetime.datetime.strptime(data_value, '%Y%m%d').strftime('%Y-%m-%d')
                    except:
                        return_data = None

    return return_data


def overdueSummaryIsNa():
    """
    当[逾期(透支)信息汇总]中“贷款逾期笔数”、“贷记卡逾期笔数”、“准贷记卡逾期笔数”均为空或0时，返回1；否则返回0
    :return:
    """
    try:
        ICROverdueSummary = formatData('ICROverdueSummary')
        Null = ['', ' ', 'nulll', 'Null', 'NULL', '/', '-', '--', '0', '0.0', 0, 0.0]

        if ICROverdueSummary == [{}]:
            return u'缺失值'
        else:
            if inquireIsReportNo(ICROverdueSummary[0]):
                count = ICROverdueSummary[0].get('count')
                count2 = ICROverdueSummary[0].get('count2')
                count3 = ICROverdueSummary[0].get('count3')

                if count in Null and count2 in Null and count3 in Null:
                    return 1
                else:
                    return 0
            else:
                return 1
    except:
        return u'缺失值'

result = overdueSummaryIsNa()

